package com.maoye.ossp.biz.statement.controller;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;
import org.json.JSONArray;
import org.json.JSONObject;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.google.common.collect.Lists;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.hotent.base.api.query.QueryFilter;
import com.maoye.ossp.biz.statement.entity.Prom;
import com.maoye.ossp.common.entity.Page;
import com.maoye.ossp.common.web.BaseController;
import com.maoye.ossp.webserviceClient.PosPayWebService;
/**
 *  
 * @author 梁涛   促销单控制类
 * @date 2014年5月20日
 * 
 */
@Controller
@RequestMapping(value = "statement/prom/")
public class PospayPromController extends BaseController {
	@RequestMapping(value = "list")
	public String list(Prom prom,HttpServletRequest request,
			HttpServletResponse response, Model model) {
		String param="{promNo:";
		param=param+prom.getPromNo()+"}";
		JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();      
		factory.setServiceClass(PosPayWebService.class);        
		factory.setAddress("http://202.105.115.147:9000/ossp-web/service/pospayWebService");               
		PosPayWebService service = (PosPayWebService) factory.create();              
		String result = service.queryByQueryFilter(param);    
		Gson gson = new Gson();
		JSONObject a = new JSONObject(result);
		Object b=a.get("data");
		JSONArray array=new JSONArray(b.toString());
		Prom varProm=new Prom();
		List<Prom> list=Lists.newArrayList(); 
		for(int i=0;i<array.length();i++){
			JSONObject json=(JSONObject)array.get(i);
			varProm.setId(Long.parseLong(json.get("id").toString()));
			varProm.setPromNo(json.getString("promNo"));
			varProm.setGoodsId(Long.parseLong(json.get("goodsId").toString()));
			varProm.setSalePrice(Double.parseDouble(json.get("salePrice").toString()));
			varProm.setPromPrice(Double.parseDouble(json.get("promPrice").toString()));
			varProm.setBuyNum(Double.parseDouble(json.get("buyNum").toString()));
			varProm.setBuyAmount(Double.parseDouble(json.get("buyAmount").toString()));
			list.add(varProm);
		}
		model.addAttribute("list",list);
		model.addAttribute("prom",prom);
		return resultPage("/biz/statement/prom/list", model, request);
	}
}
